package com.example.myexamproject;



import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.myexamproject.bean.Skin;
import com.example.myexamproject.utils.MySQLiteOpenHelper;


public class AddGamesActivity extends AppCompatActivity implements View.OnClickListener {
    //组件定义
    private EditText etskinid;
    private EditText etskinname;
    private EditText etwear;
    private EditText ettradenote;
    private EditText etprice;

    private Button btnAdd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_games);
        //初始化界面
        initView();
    }

    //初始化界面
    private void initView() {
        etskinid=(EditText)findViewById(R.id.et_skinid);
        etskinname = (EditText) findViewById(R.id.et_skinname);
        etwear = (EditText) findViewById(R.id.et_wear);
        ettradenote = (EditText) findViewById(R.id.et_tradenote);
        etprice = (EditText) findViewById(R.id.et_price);

        btnAdd = (Button) findViewById(R.id.btn_add);
        //设置按钮的点击事件
        btnAdd.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        //当单击“添加”按钮时，获取添加信息
        String skinid=etskinid.getText().toString().trim();
        String skinname = etskinname.getText().toString().trim();
        String wear = etwear.getText().toString().trim();
        String tradenote = ettradenote.getText().toString();
        String price = etprice.getText().toString();


        //检验信息是否正确
        if (TextUtils.isEmpty(skinid)) {
            Toast.makeText(this, "请输入饰品id", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(skinname)) {
            Toast.makeText(this, "请输入饰品名称", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(wear)) {
            Toast.makeText(this, "请输入饰品磨损", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(tradenote)) {
            Toast.makeText(this, "请输入备注", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(price)) {
            Toast.makeText(this, "请输入价格", Toast.LENGTH_SHORT).show();
            return;
        }

        //添加信息
        Skin game =new Skin();
        game.skinid= skinid;
        game.skinname = skinname;
        game.wear = wear;
        game.tradenote = tradenote;
        game.price = price;

        //创建数据库访问对象
        MySQLiteOpenHelper dao = new MySQLiteOpenHelper(getApplicationContext());
        dao.open();
        long result = dao.addGames(game);

        if (result > 0) {
            Toast.makeText(this, "发布成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "发布失败", Toast.LENGTH_SHORT).show();
        }
        dao.close();
        finish();

    }
}